package com.lu.model.dto;

import cn.hutool.core.util.ObjectUtil;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description: 用户角色资源
 * @Author: zhanglu
 * @Date: 2022/7/27 17:07
 */
@Data
public class UserRoleResourcesDTO {

  private Long id;
  private Long pid;
  private Long userId;
  private Long roleId;
  private Long resourceId;
  private String roleName;
  private String resourcesName;
  /**
   * 图标
   */
  private String icon;
  private String url;
  /**
   * @see com.lu.model.entity.SysResources.Type
   */
  private Integer type;

  @Data
  @Builder
  @AllArgsConstructor
  @NoArgsConstructor
  public static class TreeNodes {
    private Long id;
    private Long pid;
    /**
     * 菜单名
     */
    private String name;
    /**
     * 图标
     */
    private String icon;
    /**
     * 接口路由地址
     */
    private String url;
    /**
     * 1 目录菜单 2按钮
     * @see com.lu.model.entity.SysResources.Type
     */
    private Integer type;
    /**
     * 图标
     */
    private List<TreeNodes> childs;
  }

  public static List<TreeNodes> treeNodes(List<UserRoleResourcesDTO> resourcesList, Long pid){
    List<TreeNodes> treeNodes = new ArrayList<>();
    if(ObjectUtil.isEmpty(resourcesList)){
      return treeNodes;
    }
    if(pid == null){
      pid = 0L;
    }
    //pid 下的孩子
    Map<Long, List<UserRoleResourcesDTO>> resourcesMap = resourcesList.stream().filter(o -> o.getPid() != 0).collect(Collectors.groupingBy(UserRoleResourcesDTO::getPid));
    Long finalPid = pid;
    List<UserRoleResourcesDTO> resources = resourcesList.stream().filter(o -> {
      if(finalPid == 0L){
        return o.getPid() == 0L;
      }else {
        return o.getId().longValue() == finalPid;
      }
    }).collect(Collectors.toList());
    for (UserRoleResourcesDTO resource : resources) {  //迭代父节点
      //父节点找孩子
      List<TreeNodes> childs = treeChildNodes(null, resourcesMap, resource.getId());
      treeNodes.add(TreeNodes.builder().id(resource.getId()).pid(resource.getPid())
          .name(resource.getResourcesName())
          .icon(resource.getIcon())
          .type(resource.getType())
          .url(ObjectUtil.isNotEmpty(resource.getUrl()) ? resource.getUrl().replace("#", "/") : resource.getUrl())
          .childs(childs).build());
    }
    return treeNodes;
  }

  public static List<TreeNodes> treeChildNodes(List<TreeNodes> treeNodes, Map<Long, List<UserRoleResourcesDTO>> resourcesMap, Long pid){
    List<UserRoleResourcesDTO> resources = resourcesMap.get(pid);
    if(ObjectUtil.isEmpty(resources)){
      return treeNodes;
    }
    if(treeNodes == null){
      treeNodes = new ArrayList<>();
    }
    for (UserRoleResourcesDTO resource : resources) {
      //父节点找孩子
      List<TreeNodes> childs = treeChildNodes(null, resourcesMap, resource.getId());
      treeNodes.add(TreeNodes.builder().id(resource.getId()).pid(resource.getPid())
          .name(resource.getResourcesName())
          .icon(resource.getIcon())
          .type(resource.getType())
          .url(ObjectUtil.isNotEmpty(resource.getUrl()) ? resource.getUrl().replace("#", "/") : resource.getUrl())
          .childs(childs).build());
    }
    return treeNodes;
  }

}
